package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    /**
     * 保存游记
     * @param travel
     * @return
     */
    @PostMapping
    public JsonResult save(Travel travel) {
        JsonResult jsonResult = new JsonResult();
        try {
            travelService.save(travel);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 更新游记
     *
     * @param travel
     * @return
     */
    @PutMapping("/{id}")
    public JsonResult update(Travel travel) {
        JsonResult jsonResult = new JsonResult();
        try {
            travelService.update(travel);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 查看游记根据游记ID
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Travel get(@PathVariable Long id) {
        return travelService.get(id);
    }

    /**
     * 查看所有的已发布游记
     *
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo list(@ModelAttribute("qo") TravelQueryObject qo) {
        qo.setState(2);
        //公开状态
        qo.setIsPublic(true);
        //排序规则
        qo.setOrderBy("releaseTime DESC");

        return travelService.query(qo);
    }

}
